package com.pronhub.luowowo.mgrsite.listener;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pronhub.luowowo.article.domain.StrategyDetail;
import com.pronhub.luowowo.article.service.IStrategyDetailService;
import com.pronhub.luowowo.service.IStrategyStatisRedisService;
import com.pronhub.luowowo.util.BeanUtilLXY;
import com.pronhub.luowowo.util.Rediskey;
import com.pronhub.luowowo.vo.StrategyStatisVO;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDataDestoryListener implements ApplicationListener<ContextRefreshedEvent> {

    //从MySql数据库查出来文章的点赞数之类的Vo 然后放到Redis里面
    @Reference
    private IStrategyDetailService strategyDetailService;
    //启动后台的时候进行初始化操作
    @Reference
    private IStrategyStatisRedisService strategyStatisVORedisService;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.err.println("------------------------初始化VO--------------------");

        List<StrategyDetail> strategyDetails = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : strategyDetails) {
            //第二次启动怎么办
            //有值就跳过
            if (strategyStatisVORedisService.isStrategyStatisVOExsit(detail.getId())){
                System.err.println("是否第二次启动?");
                continue;
            }
            vo = new StrategyStatisVO();
            BeanUtilLXY.copyProperties(vo,detail);
            //设置VO没有的数值
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            System.err.println("VO第一次启动");
            //同步到Redis里面
            strategyStatisVORedisService.setStrategyStatisVO(vo);
        }
        System.err.println("------------------------初始化VO结束--------------------");



        System.err.println("------------------------初始化Zset热门--------------------");
        for (StrategyDetail detail : strategyDetails) {
            String value = Rediskey.STRATEGY_STATIS_VO.getKey(detail.id.toString());
            //热门
            if (strategyStatisVORedisService.isZsetHasValue(Rediskey.STRATEGY_ZSET_SORT_HOT.getPrefix(),value)){

                continue;
            }
            //获取他的 浏览数+评论数 总和得到这个的分数
            int score = detail.getViewnum() + detail.getReplynum();
            strategyStatisVORedisService.addHotScores(value,score);
        }
        System.err.println("------------------------初始化结束Zset热门--------------------");
        System.err.println("------------------------初始化攻略热门--------------------");
        //这个是 点赞+收藏数
        for (StrategyDetail detail : strategyDetails) {
            String value = Rediskey.STRATEGY_STATIS_VO.getKey(detail.id.toString());
            //第二次启动 如果存在 比如本来是100 再次启动就死增加
            //参数1: zset的key   参数2:zset 的value
            if (strategyStatisVORedisService.isZsetHasValue(Rediskey.STRATEGY_ZSET_SORT_COMMEND.getPrefix(),value)){

                continue;
            }
            //这个是 点赞+收藏数
            int score = detail.getThumbsupnum() + detail.getFavornum();
            strategyStatisVORedisService.addCommendScores(value,score);
        }
        System.err.println("------------------------初始化结束攻略热门--------------------");

    }
   /* public void increaseTheDataIntoRedis(List<StrategyDetail> strategyDetails,String method){
        for (StrategyDetail detail : strategyDetails) {
            String value = Rediskey.STRATEGY_STATIS_VO.getKey(detail.id.toString());
            if (method.equals(Rediskey.STRATEGY_ZSET_SORT_COMMEND.getPrefix())){
                if (strategyStatisVORedisService.isZsetHasValue(Rediskey.STRATEGY_ZSET_SORT_COMMEND.getPrefix(),value)){
                    continue;
                }
            }else {
                if (strategyStatisVORedisService.isZsetHasValue(Rediskey.STRATEGY_ZSET_SORT_HOT.getPrefix(),value)){

                    continue;
                }
            }
            if (method.equals(Rediskey.STRATEGY_ZSET_SORT_COMMEND.getPrefix())){
                int score = detail.getThumbsupnum() + detail.getFavornum();
            }else {
                int score = detail.getViewnum() + detail.getReplynum();
            }
        }
    }*/
   /* private void initStrategyStatisZset(List <StrategyDetail> list, RedisKeys zsetRey) {
        String setkey = zsetRey.getPrifx();
        int score;
        for (StrategyDetail d : list) {
            String zvalue = RedisKeys.STRATEGY_STATIS_VO.join( d.getId().toString() );

            if (strategyStatisVOService.checkStrategyRankTopValue( setkey, zvalue )) {
                continue;
            }
            if (zsetRey == RedisKeys.STRATEGY_STATIS_VO) {
                score = d.getReplynum() + d.getViewnum();
            } else {
                score = d.getReplynum() + d.getViewnum();
            }
            strategyStatisVOService.initStrategyHotTop( setkey, zvalue, score );
        }
    }*/

}
